package com.ruoyi.patrol.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.patrol.service.IPtCatalogService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.patrol.domain.PtProject;
import com.ruoyi.patrol.service.IPtProjectService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目信息Controller
 * 
 * @author linan
 * @date 2024-02-03
 */
@RestController
@RequestMapping("/resource/project")
public class PtProjectController extends BaseController
{
    @Autowired
    private IPtProjectService ptProjectService;

    @Autowired
    private IPtCatalogService catalogService;

    /**
     * 查询项目信息列表
     */
    @PreAuthorize("@ss.hasPermi('resource:project:list')")
    @GetMapping("/list")
    public TableDataInfo list(PtProject ptProject)
    {
        startPage();
        List<PtProject> list = ptProjectService.selectPtProjectList(ptProject);
        return getDataTable(list);
    }

    /**
     * 导出项目信息列表
     */
    @PreAuthorize("@ss.hasPermi('resource:project:export')")
    @Log(title = "项目信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PtProject ptProject)
    {
        List<PtProject> list = ptProjectService.selectPtProjectList(ptProject);
        ExcelUtil<PtProject> util = new ExcelUtil<PtProject>(PtProject.class);
        util.exportExcel(response, list, "项目信息数据");
    }

    /**
     * 获取项目信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('resource:project:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        PtProject ptProject = ptProjectService.selectPtProjectById(id);

        Long catId = ptProject.getCatId();
        if(catId != null) {
            Long[] catalogPath = catalogService.getCatalogPath(catId);
            ptProject.setCatalogPath(catalogPath);
        }

        return success(ptProject);
    }

    /**
     * 新增项目信息
     */
    @PreAuthorize("@ss.hasPermi('resource:project:add')")
    @Log(title = "项目信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PtProject ptProject)
    {
        return toAjax(ptProjectService.insertPtProject(ptProject));
    }

    /**
     * 修改项目信息
     */
    @PreAuthorize("@ss.hasPermi('resource:project:edit')")
    @Log(title = "项目信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PtProject ptProject)
    {
        return toAjax(ptProjectService.updatePtProject(ptProject));
    }

    /**
     * 删除项目信息
     */
    @PreAuthorize("@ss.hasPermi('resource:project:remove')")
    @Log(title = "项目信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ptProjectService.deletePtProjectByIds(ids));
    }
}
